import pygame
from pygame.sprite import Sprite


class Bullet(Sprite):

    def __init__(self, ai_game):
        super().__init__()
        # 记录游戏界面
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = ai_game.settings.bullet_color

        # 绘制子弹矩形
        self.image = pygame.transform.scale(pygame.image.load('images/bullet.bmp'), (30, 40))
        self.rect = self.image.get_rect()
        # 定位子弹位置在飞船中上处
        self.rect.midtop = ai_game.ship.rect.midtop

        # 精确控制子弹位置
        self.y = float(self.rect.y)

    def create_bullet(self):
        """ 创建子弹 """
        self.screen.blit(self.image, self.rect)
        # pygame.draw.rect(self.screen, self.color, self.rect)

    def update(self):
        """ 子弹移动 """
        self.y -= self.settings.bullet_speed
        self.rect.y = self.y
